<?php
namespace Index\Controller;
use Think\Controller;
class QuickloginController extends Controller {


    public function qq_callback(){
        //引入QQ登陆类
        import('ORG.QQ.qqConnectAPI');
        $QC = new \QC();
        $token  = $QC->qq_callback();
        $openid = $QC->get_openid();
        $QC = new \QC($token,$openid);
        $arr = $QC->get_user_info();
        dump($token);
        dump($openid);
        dump($arr);
        //$str = var_export($arr,TRUE);
        // file_put_contents("qqqqqqqqqqqq.txt", print_r($arr,true));
        // file_put_contents("qqqqqqqq.txt",  $openid);

        //业务代码不要
        /*$db=M("personal");
        $where['qq_openid']=$openid;
        //判断此QQ是否注册 唯一标识符openid 在表里加了个qq_openid为唯一哈
        $isqq=$db->where($where)->find();
        if($isqq){
            //写入登陆状态
            session('id', $isqq['id']);//ID
            session('name', $isqq['user']);  //名
            session('type', 'personal');   //类别
            session('state',1);   //状态
            $aa =  $this->isMobile();
            if($aa == '1'){
                exit( '<script>alert("登陆成功!");location.href="/Index/Wmember/wmember.html";</script>');
            }else{
                $this->success('登陆成功！',U('Member/wmember'));
            }

        }
        else {
            $data['qq_openid']=$openid; //QQ登陆唯openid
            $data['user'] = preg_replace('|[^a-zA-Z0-9\x{4e00}-\x{9fa5}]|u','',$arr['nickname']); //用户名


            $data['nickname']=$arr['nickname']; //网名
            if ($arr['figureurl_qq_2']) {
                $data['img_url']=$arr['figureurl_qq_2']; //头像
            }else{
                $data['img_url']=$arr['figureurl_qq_1']; //头像
            }

            $data['childsex']=$arr['gender']; //性别
            $data['createtime']=time(); //性别
            $data['status']='1'; //用户状态为启用
            //如果用户名存在
            $name['user']=$data['user'];
            $isname=$db->where($name)->find();
            if($isname){
                //用户名存在添加随机数
                $data['user']=$arr['nickname']."_".rand(1000,9999);
            }
            //判断是否写入成功
            if($id=$db->add($data)){
                //$info=$db->where('id = "'.$id.'"')->find();
                //写入登陆状态
                session('id', $id);//ID
                session('name', $data['user']); //名
                session('type', 'personal');//类别
                session('state',1);   //状态

                //session('email', '未填写');
                //session('lastLoginTime', time());
                //session('login_count', '1');
                $aa =  $this->isMobile();
                if($aa == '1'){
                    exit( '<script>alert("注册成功!");location.href="/Index/Wmember/wmember.html";</script>');
                }else{
                    $this->success('注册成功！',U('Member/wmember'));
                }

            }else{
                $aa =  $this->isMobile();
                if($aa == '1'){
                    exit( '<script>alert("注册失败!");location.href="/Index/Wmember/login.html";</script>');
                }else{
                    $this->error('注册失败！',U('Member/login'));
                }

            }
        }*/
    }

    public function wb_callback(){
        $aa =  $this->isMobile();

        if ($_GET['code']) {
            //引入微博登陆类
            import('ORG.Weibo.wbConnectAPI');
            //实例化
            $o = new \SaeTOAuthV2( WB_AKEY , WB_SKEY );
            $keys = array();
            $keys['code'] = $_GET['code'];
            $keys['redirect_uri'] = WB_CALLBACK_URL;
            $token = $o->getAccessToken( 'code', $keys ) ;

            if ($token) {
                //$_SESSION['token'] = $token;
                //setcookie( 'weibojs_'.$o->client_id, http_build_query($token) );
                //引入微博登陆类
                //import('ORG.Weibo.wbConnectAPI');
                //实例化
                $c = new \SaeTClientV2( WB_AKEY , WB_SKEY , $token['access_token'] );
                //$ms  = $c->home_timeline(); // 获取当前登录用户及其所关注用户的最新微博消息。
                $uid_get = $c->get_uid();
                $uid = $uid_get['uid'];
                $user_message = $c->show_user_by_id( $uid);//根据ID获取用户等基本信息

                dump($keys);
                dump($token);
                dump($user_message);
                //业务代码不要del
                /*
                $openid = $user_message['idstr'];
                $db=M("personal");
                $where['wb_openid']=$openid;
                //判断此微博是否注册 唯一标识符openid 在表里加了个wb_openid为唯一哈
                $iswb=$db->where($where)->find();
                if($iswb){
                    //写入登陆状态
                    session('id', $iswb['id']);//ID
                    session('name', $iswb['user']);  //名
                    session('type', 'personal');   //类别
                    session('state',1);   //状态
                    if($aa == '1'){
                        exit( '<script>alert("登陆成功!");location.href="/Index/Wmember/wmember.html";</script>');
                    }else{
                        $this->success('登陆成功！',U('Member/wmember'),1);
                    }

                }else{
                    $data['wb_openid']=$openid; //微博登陆唯openid
                    $data['user']= preg_replace('|[^a-zA-Z0-9\x{4e00}-\x{9fa5}]|u','',$user_message['name']);; //用户名
                    $data['img_url']= $user_message['profile_image_url']; //头像

                    switch ($user_message['gender'])
                    {
                        case m:
                            $data['childsex']='男'; //性别
                            break;
                        case f:
                            $data['childsex']='女'; //性别
                            break;
                        default:
                            $data['childsex']='未知'; //性别
                    }


                    $data['createtime']=time(); //注册时间
                    $data['status']='1'; //用户状态为启用
                    //如果用户名存在
                    $name['user'] = $data['user'];
                    $isname=$db->where($name)->find();
                    if($isname){
                        //用户名存在添加随机数
                        $data['user']=$user_message['name']."_".rand(1000,9999);
                    }
                    //判断是否写入成功
                    if($id=$db->add($data)){
                        //$info=$db->where('id = "'.$id.'"')->find();
                        //写入登陆状态
                        session('id', $id);//ID
                        session('name', $data['user']); //名
                        session('type', 'personal');//类别
                        session('state',1);   //状态

                        //session('email', '未填写');
                        //session('lastLoginTime', time());
                        //session('login_count', '1');
                        if($aa == '1'){
                            exit( '<script>alert("注册成功！");location.href="/Index/Wmember/wmember.html";</script>');
                        }else{
                            $this->success('注册成功！',U('Member/wmember'),1);
                        }

                    }else{
                        if($aa == '1'){
                            exit( '<script>alert("注册失败！");location.href="/Index/Wmember/login.html";</script>');
                        }else{
                            $this->error('注册失败！',U('Member/login'),1);
                        }

                    }
                }
            }else{
                if($aa == '1'){
                    exit( '<script>alert("授权失败,请重新授权！");location.href="/Index/Wmember/login.html";</script>');
                }else{
                    $this->error('授权失败,请重新授权！',U('Member/login'),1);
                }
            */
            }
        }else{
            /*if($aa == '1'){
                exit( '<script>alert("注册失败!");location.href="/Index/Wmember/login.html";</script>');
            }else{
                $this->error('授权失败，请重新授权！',U('Member/login'),1);
            }*/

        }

    }

    public	function isMobile() {
        // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
        if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
            return true;
        }
        // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
        if (isset($_SERVER['HTTP_VIA'])) {
            // 找不到为flase,否则为true
            return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
        }
        // 脑残法，判断手机发送的客户端标志,兼容性有待提高。其中'MicroMessenger'是电脑微信
        if (isset($_SERVER['HTTP_USER_AGENT'])) {
            $clientkeywords = array('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile','MicroMessenger');
            // 从HTTP_USER_AGENT中查找手机浏览器的关键字
            if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
                return true;
            }
        }
        // 协议法，因为有可能不准确，放到最后判断
        if (isset ($_SERVER['HTTP_ACCEPT'])) {
            // 如果只支持wml并且不支持html那一定是移动设备
            // 如果支持wml和html但是wml在html之前则是移动设备
            if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
                return true;
            }
        }
        return false;
    }






}